/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.dc.controller;

import com.cxhd.dc.DataConfig;
import com.cxhd.dc.tool.FileUtil;
import com.cxhd.dc.tool.HttpClientUt;
import com.cxhd.dc.tool.ZipFileTool;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

/**
 *
 * @author Administrator
 */
public class CenterPatientController extends Controller {

    public void index() {
        // render("/patient/patientList.jsp");
        System.out.println(DataConfig.getInstance().getAtlasServer());
        render("/center/centerList.jsp");
    }

    public void getPatient() {
        String hospital_sn = getPara("sn");
        String sql = "select * ";
        String serarchContent = getPara("content", null);
        String param = "from patient where hospital_sn='" + hospital_sn + "' order by createtime desc";
        if (serarchContent != null) {
            param = "from patient  where pat_name like '%" + serarchContent + "%' or pat_id like '%" + serarchContent + "%' order by createtime desc";

        }

        Page<Record> userPage = Db.use("atlasCenter").paginate(getParaToInt("currentPage"), getParaToInt("pageSize", 10), sql, param);

        renderJson(userPage);
    }

    public void getCenterPatientList() {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("sn", DataConfig.getInstance().getLocalhostSN()));
        params.add(new BasicNameValuePair("content", getPara("content")));
        params.add(new BasicNameValuePair("currentPage", getPara("currentPage")));
        params.add(new BasicNameValuePair("pageSize", getPara("pageSize")));
        System.out.println("http://" + DataConfig.getInstance().getAtlasServer() + "/DataConfig/center/getPatient");
        renderJson(HttpClientUt.httpPost("http://" + DataConfig.getInstance().getAtlasServer() + "/DataConfig/center/getPatient", params));

    }

    public void downloadFileFromCenter() {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("sn", DataConfig.getInstance().getLocalhostSN()));
        params.add(new BasicNameValuePair("pks", getPara("pks")));

        renderText(HttpClientUt.httpPost("http://" + DataConfig.getInstance().getAtlasServer() + "/DataConfig/center/downloadPatient", params));

    }

    public void downloadPatient() {
        String hospital_sn = getPara("sn");

        String pks = getPara("pks");
        try {
            if (hospital_sn != null && pks != null) {
                List<Record> study_list = Db.use("atlasCenter").find("select a.study_iuid from study a left join patient b on a.patient_fk=b.pk where b.pk in (" + pks + ") and hospital_sn='" + hospital_sn + "'");
                if (study_list != null && !study_list.isEmpty()) {
                    for (Record study : study_list) {
                        List<Record> file_list = Db.use("atlasCenter").find("select * from files where study_iuid='" + study.getStr("study_iuid") + "'");
                        //获取file
                        for (Record file : file_list) {
                            File newFile = new File(DataConfig.getInstance().getBasePath() + "/" + DataConfig.getInstance().getOtherFileFolder() + file.getStr("filename"));

                            download(newFile.getAbsolutePath(), file.getStr("file_id"));

                            String zipName = hospital_sn + "_" + newFile.getName() + "_" + DataConfig.getInstance().getLocalhostSN() + ".u";

                            String zipPath = DataConfig.getInstance().getBasePath() + "/" + DataConfig.getInstance().getZipFolder() + zipName;
                            String password = FileUtil.makePSWD(zipName.substring(0, zipName.lastIndexOf(".")));
                            ZipFileTool.upZipFile(newFile, zipPath, password, true, zipPath);
                        }
                    }
                    renderText("true");

                }
            }
        } catch (Exception e) {

            e.printStackTrace();
            renderText("false");
        }
    }

    private void download(String filepath, String fileId) {
        System.out.println("fileserverUrl:" + DataConfig.getInstance().getAtlasFileServer());
        HttpClientUt.download(filepath, "http://" + DataConfig.getInstance().getAtlasFileServer() + "/CXFS/download?fileId=" + fileId);

    }

}
